package edu.hit.stu.hander.exception;

import edu.hit.stu.util.common.IPrint;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@ControllerAdvice
public class IExceptionHandler {

    //指定出现什么异常值执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Mono<String> defaultExceptionHandler(ServerWebExchange exchange, Exception ex) {
        ServerHttpRequest request = exchange.getRequest();
        String baseResult = "发现全局异常，正在处理，请刷新再试";
        IPrint.printError(ex.getMessage() + "\n" +
                "客户地址:" + request.getRemoteAddress() + "\n" +
                "请求地址:" + request.getURI());
        baseResult += ex.getMessage();
        return Mono.just(baseResult);
    }

    //指定TokenException异常值执行这个方法
    @ExceptionHandler(WebFluxException.class)
    @ResponseBody//为了返回数据
    public Mono<String> webExceptionHandler(ServerWebExchange exchange, WebFluxException ex) {
        String baseResult = "Token异常，请刷新重试";
        ServerHttpRequest request = exchange.getRequest();
        IPrint.printError(ex.getMessage() + "\n" +
                "客户地址:\t" + request.getRemoteAddress() + "\n" +
                "请求地址:\t" + request.getURI());
        baseResult += ex.getMessage();
        return Mono.just(baseResult);
    }
}
